<?php

namespace Addons\OaSystem\Controller;
use Addons\OaSystem\Controller\OaDutyBaseController;

class OaUserRoleController extends OaDutyBaseController
{
    protected $model;

    function _initialize()
    {
        $this->model = $this->getModel('oa_user_role');
        parent::_initialize();

    }
    function lists(){
        $emp_id = $_GET['emp_id'];

        $res['title'] = '新增';
        $res['is_buttion'] = false;
        $res['url'] = addons_url ( 'OaSystem://OaUserRole/add' ,array('mdm'=>I('mdm'), 'emp_id'=>$emp_id));
        $top_more_button[] = $res;
        $res['title'] = '树形显示';
        $res['is_buttion'] = false;
        $res ['url'] = addons_url ( 'OaSystem://OaUserRole/tree' ,array('mdm'=>I('mdm')));
        $top_more_button[] =$res;

        $this->assign('top_more_button',$top_more_button);
        $this->assign('add_button',true);

        //解析列表规则
        $list_data = $this->_get_model_list($this->model);
        $grids = $list_data['list_grids'];
        $data = $list_data['list_data'];

        foreach ($data as $i => $v) {
            $user = db_find('oa_employee', ['id'=>$v['emp_id']]);
            $dept = db_find('oa_dept', ['id'=>$user['dept_id']]);
            $role = db_find('oa_role', ['id'=>$v['role_id']]);
            $user_assign = db_find('oa_employee', ['id'=>$v['assigned_by']]);

            $data[$i]['emp_id'] = $user['name'].' ['.$dept['name'].']';
            $data[$i]['role_id'] = $role['name'];
            $data[$i]['assigned_by'] = $user_assign['name'];
        }
        if(empty($data)) {
            $user = db_find('oa_employee', ['id'=>$emp_id]);
            $data[0]['emp_id'] = $user['name'];
        }
        $this->assign( 'list_grids', $grids);
        $this->assign('list_data',  $data);
        $this->display();
    }

    function tree(){
        $depts = D("OaDeptView")->order('grade_value')->select();
        $tree = array();
        foreach ($depts as $dept) {
            if(empty($dept['pid'])) {
                $tree[$dept['id']] = $this->buildTreeNode($dept);
            }
            else {
                $this->buildMemberTree($tree, $dept);
            }
        }
        $memberTreeHtmlStr = $this->buildMemberTreeHtml($tree);
        $this->assign('membershipTree', $memberTreeHtmlStr);
        $this->display();
    }

    function buildTreeNode($dept) {
        $members = db_select('oa_employee', ['dept_id'=>$dept['id']], null, 'id,name');
        foreach ($members as $i => $member) {
            $roles = array();
            //array_push($roles, array('name'=>'角色管理->'));
            $user_roles = db_select('oa_user_role', ['emp_id'=>$member['id']]);
            foreach ($user_roles as $user_role) {
                $role = db_find('oa_role', ['id'=>$user_role['role_id']], null, 'name');
                if(!empty($user_role['assigned_by'])) {
                    $assignedBy = db_find('oa_employee', ['id'=>$user_role['assigned_by']]);
                    $role['assigned_by'] = $assignedBy['name'];
                }
                array_push($roles, $role);
            }
            $members[$i]['roles'] = $roles;
        }
        $node = array (
            'name'=>$dept['name'],
            'member'=>$members,
            'subDept'=>array(),
        );
        return $node;
    }

    function buildMemberTree(&$tree, $dept) {
        //find father node
        foreach ($tree as $i => $node) {
            if($dept['pid'] == $i) {
                $tree[$i]['subDept'][$dept['id']] = $this->buildTreeNode($dept);
                return true;
            }
        }
        foreach ($tree as $i => $node) {
            if(!empty($node['subDept'])) {
                if($this->buildMemberTree($tree[$i]['subDept'], $dept))
                    return true;
            }
        }
        return false;
    }

    function buildMemberTreeHtml($tree) {
        $treeHtmlStr = '';
        foreach ($tree as $node) {
            $treeHtmlStr .= '<li><span class="folder" style="font-weight:bold;color:#002b36">'.$node['name'].'</span>';
            $treeHtmlStr .= '<ul>';
            foreach ($node['member'] as $member) {
                $treeHtmlStr .= '<li><span class="folder" style="color:#0fb1da">'.'<a href="'.U('lists',array('emp_id'=>$member['id'])).'">'.$member['name'].'</a></span>';
                $treeHtmlStr .= '<ul>';
                foreach ($member['roles'] as $role) {
                    if(empty($role['assigned_by']))
                        $treeHtmlStr .= '<li><span class="file" style="color:#00CC33">'.$role['name'].'</span>';
                    else
                        $treeHtmlStr .= '<li><span class="file" style="color:#00CC33">'.$role['name'].' <-'.$role['assigned_by'].'</span>';
                }
                $treeHtmlStr .= '</ul></li>';
            }
            if(!empty($node['subDept'])) {
                $treeHtmlStr .= $this->buildMemberTreeHtml($node['subDept']);
            }
            $treeHtmlStr .= '</ul></li>';
        }
        return $treeHtmlStr;
    }

    function treeview(){
//        $user_roles = array();
//        $users = db_select('oa_employee');
//        foreach ($users as $i => $v1) {
//            $user_roles[$i]['name'] = $v1['name'];
//            $duty = db_find('oa_duty', array('id'=>$v1['duty_id']));
//            $dutyRole = db_select('oa_role_duty', array('duty_id'=>$v1['duty_id']));
//            $j = 0;
//            if(!empty($duty)) {
//                $user_roles[$i]['children'][$j]['name'] = "职务角色[".$duty['name']."]";
//                foreach ($dutyRole as $k => $v2) {
//                    $role = db_find('oa_role', array('id'=>$v2['role_id']));
//                    $user_roles[$i]['children'][$j]['children'][$k]['name'] = $role['name'];
//                }
//                $j += 1;
//            }
//            $assignedRole = db_select('oa_user_role', array('emp_id'=>$v1['id']));
//            if(!empty($assignedRole)) {
//                $user_roles[$i]['children'][$j]['name'] = "指定角色";
//                foreach ($assignedRole as $k => $v2) {
//                    $role = db_find('oa_role', array('id'=>$v2['role_id']));
//                    $assignedBy = db_find('oa_employee', array('id'=>$v2['assigned_by']));
//                    $user_roles[$i]['children'][$j]['children'][$k]['name'] = $role['name']." <- ".$assignedBy['name'];
//                }
//            }
//        }
//        $this->assign('user_roles', $user_roles);
//        $this->display();
    }

    function add(){
        if(IS_POST) {
            $data['appid'] = $this->appId;
            $data['emp_id'] = $_POST['emp_id'];
            $roles = $this->getRoles();
            foreach($roles as $vo) {
                if($_POST[$vo['name']] == 1) {
                    $rolename = ($_POST['dept']=='全部')?$vo['name']:($_POST['dept'].'_'.$vo['name']);
                    if(($role = db_find('oa_role', ['name'=>$rolename])) == NULL) {
                        $this->error('找不到角色-'.$rolename);
                        return false;
                    }
                    $data['role_id'] = $role['id'];
                    if(!db_find('oa_user_role', $data)) {
                        $user = db_find('oa_employee', ['userid'=>$this->mid]);
                        $data['assigned_by'] = empty($user) ? 0 : $user['id'];
                        M('oa_user_role')->add($data);
                    }
                }
            }
            $this->success('添加成功！', U ( 'lists', array('emp_id'=>$data['emp_id'])), 600);
        } else {
            $emp_id = $_GET['emp_id'];
            $employee = db_find('oa_employee', ['id'=>$emp_id]);
            $roles = $this->getRoles();
            $depts = $this->getDepts();
            $this->assign('emp', $employee);
            $this->assign('roles', $roles);
            $this->assign('depts', $depts);
            $this->display('add');
        }
    }

}